package com.rnkrsoft.suyan.config.server.services.menu.domains;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import javax.doc.data.Response;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
@Data
public class FetchMenuByUserIdResponse extends Response {
    final List<Menu> menus = new ArrayList<Menu>();

    public FetchMenuByUserIdResponse addMenu(Menu menu){
        this.menus.add(menu);
        return this;
    }

    @Data
    @Builder
    @AllArgsConstructor
    @NoArgsConstructor
    public static class Menu implements Serializable {
        String icon;
        String title;
        String application;
        String product;
        final List<MenuItem> items = new ArrayList<MenuItem>();

        public Menu addItem(MenuItem menuItem){
            this.items.add(menuItem);
            return this;
        }

        public boolean isHasAuthority(){
            return !items.isEmpty();
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;

            Menu menu = (Menu) o;

            if (!application.equals(menu.application)) return false;
            return product.equals(menu.product);
        }

        @Override
        public int hashCode() {
            int result = application.hashCode();
            result = 31 * result + product.hashCode();
            return result;
        }
    }

    @Data
    @Builder
    @AllArgsConstructor
    @NoArgsConstructor
    public static class MenuItem implements Serializable {
        String icon;
        String title;
        boolean hasAuthority;
        String application;
        String product;
        String action;
        String version;

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;

            MenuItem menuItem = (MenuItem) o;

            if (!application.equals(menuItem.application)) return false;
            if (!product.equals(menuItem.product)) return false;
            if (!action.equals(menuItem.action)) return false;
            return version.equals(menuItem.version);
        }

        @Override
        public int hashCode() {
            int result = application.hashCode();
            result = 31 * result + product.hashCode();
            result = 31 * result + action.hashCode();
            result = 31 * result + version.hashCode();
            return result;
        }
    }
}
